package GUI;

import java.awt.*;

import javax.swing.*;
import javax.imageio.ImageIO;
import javax.swing.JTextField;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;

import Client.ClientHendler;
import Client.HTSMain;
import Logic.LoginInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;


/**
 * this class implements gui of login window 
 * @author Gabi Revah
 */
public class Login extends JPanel {
	
	private JTextField usertextField;
	private JPasswordField passtextField;
	private JTextField PortTextField;
	private JTextField IPTextField;
	private JButton btnLogin;
	static LoginInfo loginInfo=null;
	static String un=null;
	static String pas=null;
	private ClientHendler client;

public Login() throws IOException{
			
			initComponents();
		}
		
		
		public void initComponents() throws IOException{
			
			
			//setBackground(new Color(153, 204, 255));
			setSize(1081,758);

			JLabel lblUserId = new JLabel("User ID");
			lblUserId.setBounds(345, 298, 57, 19);
			lblUserId.setFont(new Font("Tahoma", Font.BOLD, 15));
			
			JLabel lblPassword = new JLabel("Password");
			lblPassword.setBounds(345, 340, 73, 19);
			lblPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
			
			JLabel lblPort = new JLabel("Port");
			lblPort.setBounds(345, 382, 32, 19);
			lblPort.setFont(new Font("Tahoma", Font.BOLD, 15));
			
			JLabel lblIp = new JLabel("IP");
			lblIp.setBounds(345, 427, 17, 19);
			lblIp.setFont(new Font("Tahoma", Font.BOLD, 15));
			
			JButton btnExit = new JButton("EXIT");
			btnExit.setBounds(884, 651, 116, 44);
			btnExit.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 13));
			btnExit.addActionListener(new java.awt.event.ActionListener() {     // Login action listener 
	            public void actionPerformed(java.awt.event.ActionEvent evt) {
	            	btnExitActionPerformed(evt);
	            
	            }

				private void btnExitActionPerformed(ActionEvent evt) {
					System.exit(0);
					
				}
	        });
			
			 btnLogin = new JButton("Login");
			
			btnLogin.setBounds(345, 471, 257, 36);
			btnLogin.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			
			
			usertextField = new JTextField();
			usertextField.setFont(new Font("Tahoma", Font.PLAIN, 14));
			usertextField.setBounds(464, 292, 138, 31);
			usertextField.setColumns(10);
			
			PortTextField = new JTextField();
			PortTextField.setBounds(464, 378, 138, 31);
			PortTextField.setColumns(10);
			
			IPTextField = new JTextField();
			IPTextField.setBackground(new Color(255, 255, 255));
			IPTextField.setBounds(464, 420, 138, 31);
			IPTextField.setColumns(10);
			
			passtextField = new JPasswordField();
			passtextField.setBounds(464, 336, 138, 31);
			setLayout(null);
			add(btnExit);
			add(btnLogin);
			add(lblUserId);
			add(lblPort);
			add(lblIp);
			add(lblPassword);
			add(IPTextField);
			add(PortTextField);
			add(usertextField);
			add(passtextField);
			
			JLabel lblLogo = new JLabel("");
			lblLogo.setIcon(new ImageIcon(Login.class.getResource("/Images/eye4.png")));
			lblLogo.setFont(new Font("Tahoma", Font.BOLD, 82));
			lblLogo.setBounds(262, 31, 589, 238);
			add(lblLogo);
			
			JLabel lblNewLabel = new JLabel("");
			lblNewLabel.setIcon(new ImageIcon(Login.class.getResource("/Images/HTS_logo.png")));
			lblNewLabel.setBounds(736, 11, 301, 67);
			add(lblNewLabel);
			
			
		
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				 int Port= 0;
					String IP= null;
					
					
					String userName =usertextField.getText();  //get user name text field 
					String password =passtextField.getText();  //get password text field 
					
					if (userName.equals("") ) 
					{
						JOptionPane.showMessageDialog(null, "Please enter a user name ", "Error",JOptionPane.ERROR_MESSAGE);
							
					}
					
					else if (password.equals("") ) 
					{
						JOptionPane.showMessageDialog(null, "Please enter a password","Error",JOptionPane.ERROR_MESSAGE);
						
					}
					
					
					else	if (    (!(userName.equals(""))) || (!( password.equals("")))    )
							{
					loginInfo = new LoginInfo(userName , password ); // save login information
					un=userName;
					pas=password;
					
					
					
					if (PortTextField.getText().equals("")) Port= 5555;  // define port 
					else Port = Integer.parseInt(PortTextField.getText());
					
					if (IPTextField.getText().equals("")) IP="127.0.0.1"; // define ip 
					else IP= IPTextField.getText();
					
					HTSMain.port= Port;
					HTSMain.host= IP;
					
					
					
				
				    
				    
					ArrayList<String> Arr=new ArrayList<String>();
			        Arr.add("Login");
			        Arr.add(userName);
			        Arr.add(password);
			        try {
						client = new ClientHendler( IP , Port);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
			        HTSMain.chat = client;
			       
			        client.handleMessageFromGui(Arr);
					Login.this.setVisible(false);
					
					
						}
				
				
			}
		});
		
		}
		
/**
 * this method get the client from the client hendler 
 * @return the client 
 */
		public ClientHendler getClient() {
			return client;
		}

/**
 * this method get the client from the client hendler
 * @param client
 */
		public void setClient(ClientHendler client) {
			this.client = client;
		}
}